#include "ShootingEnemy.h"
#include "../DeadlyShadowsLastHope/Player.h"
#include "../DeadlyShadowsLastHope/DestroyEntityMsg.h"
#include"../GameplayStates/GameplayState.h"

CShootingEnemy::CShootingEnemy(void)
{

}

CShootingEnemy::~CShootingEnemy(void)
{
				CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
		CMessageSystem::GetInstance()->SendMsg(pMsg);
		pMsg = nullptr;
}

//Inherited from CEntity///////////////////////////////
void CShootingEnemy::Update (float fElapsedTime)
{
	
	CEntity::Update(fElapsedTime);
	m_pAM->Update(fElapsedTime,this->m_cAniInfo);
	if(this->GetPosY() + this->GetHeight() < CGame::GetInstance()->GetHeight() && this->OnPlatform() == false)
	{
		this->SetVelY(this->GetVelY()+5);
	}
	if(this->GetPosY() + this->GetHeight() > (int)CGame::GetInstance()->GetHeight())
	{
		this->SetPosY((float)CGame::GetInstance()->GetHeight()-this->GetHeight());
	}

		if(this->GetPosX() <= 0)
		if(this->GetVelX() < 0)
		{
			this->SetVelX(0);
			this->SetPosX(0);
		}

		if(this->GetPosX() + this->GetWidth() >= CGame::GetInstance()->GetWidth())
			if(this->GetVelX() > 0)
			{
				this->SetVelX(0);
				this->SetPosX((float)CGame::GetInstance()->GetWidth()-this->GetWidth());
			}
}

void CShootingEnemy::Render (void)
{
		this->m_pD3D->GetSprite()->Flush();
	RECT playerRect = {this->GetRect().left - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().top,
		this->GetRect().right - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().bottom};
	m_pD3D->DrawRect(playerRect, 0, 255, 255);
m_pAM->Render(m_cAniInfo,playerRect.left-15,playerRect.top+3,1,this->GetFacing());
}

RECT CShootingEnemy::GetRect(void) const
{
	RECT rSelf = {(LONG)GetPosX(),	(LONG)GetPosY(),	(LONG)(GetPosX() + GetWidth()),	(LONG)(GetPosY() + GetHeight())	};
	return rSelf;
}

bool CShootingEnemy::CheckCollision( IEntity* pOther )
{
				if(CEntity::CheckCollision(pOther) == false)
		return false;
	RECT me = this->GetRect();
	RECT other = pOther->GetRect();
	RECT temp;
	IntersectRect(&temp, &me, &other);
	switch(pOther->GetType())
	{
	case OBJ_TILE:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.left-this->GetWidth());
				}

				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.right);
				}
			}
	if(this->GetVelY() > 0)
    {
		if(temp.bottom !=0)
		{
			this->OnPlatform(true);

			if(this->GetVelY() > 0)
				this->SetVelY(0); 
		}
	}
		}
		break;
	case OBJ_PLAYER:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.left-this->GetWidth());
/*
				SOund/damage events
*/ 
				}
				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.right);
					/*
					Send sound and damage events
					*/ 
				}
			}
		}
		break;
	}

	return true;
}
///////////////////////////////////////////////////////

void CShootingEnemy::HandleEvent(CNEvent* pEvent)
{
		if(pEvent->GetEventID() == "take damage")
	{
		this->m_nHealth -= 35;
		CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pTarget);
		pPlayer->SetCurrentHits(pPlayer->GetCurrentHits()+1);
		this->SetVelX(-(this->GetVelX()+10));
	}
	if(pEvent->GetEventID() == "Return")
	{
		CEventSystem* self = CEventSystem::GetInstance();
		self->SendEvent("TakePlayerDamage");
		m_cAniInfo.SetName("EShootWalk");	
		m_cAniInfo.SetNext("EShootAttack");
		m_cAniInfo.SetTime(0);
		m_cAniInfo.SetFrame(0);
		m_battack=false;


	}
}

bool CShootingEnemy::Init(CEntity* pTarget,int x,int y,int speedset)
{
		if(pTarget->GetType() != OBJ_PLAYER)
		return false;
		else
		{
	m_pES = CEventSystem::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pMS	= CMessageSystem::GetInstance();
	m_pAM	= CAnimationManager::GetInstance();

//	CEventSystem::GetInstance()->RegisterClient("take damage", this);
//	CEventSystem::GetInstance()->RegisterClient("Return", this);


	//Set planned animations
	this->m_cAniInfo.SetName("EShootWalk");		 //Specific to class of enemy
	this->m_cAniInfo.SetNext("EShootAttack");	 //Specific to class of enemy
	this->m_cAniInfo.SetTime(0);			 //Specific to class of enemy
	this->m_cAniInfo.SetFrame(0);			 //Specific to class of enemy

	this->SetHeight(m_pAM->GetCHeight(this->m_cAniInfo));
	this->SetWidth(m_pAM->GetCWidth(this->m_cAniInfo));

	this->SetFacing(true);
	this->SetDensity(1);

	this->SetPosX(float(x));
	this->SetPosY(float(y));//(float)CGame::GetInstance()->GetHeight() - this->GetHeight()-500);
	this->m_eType = OBJ_SHOOT_ENEMY;
	this->SetVelX(0);
	this->SetVelY(0);
	this->m_pTarget = pTarget;
	this->m_nHealth = 100;
	this->m_nSpeedoffset =speedset;
	this->OnPlatform(false);

	this->m_fTimer =0;
	this->m_fAttackTimer =0.75f;
	this->m_battack=false;
	}
	return true;
		
}

//=--------------------------------------------------------------------------------
//THE SHIT I WROTE-----------------------------------------------------------------
//---------------------------------------------------------------------------------
/*
#include "FlyingEnemy.h"
#include "Game.h"
#include "CreateBulletMessage.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "Bullet.h"

#define SHOT_DELAY 0.25f
#define MOVEMENT_DELAY 2.5f

CFlyingEnemy::CFlyingEnemy(void)
{
	m_eType = OBJ_FLYING_ENEMY;

	m_bFaceing			= 0;
	m_fTimer			= 0;
	m_nHealth			= 0;
	m_fShootTimer		= 0;
	m_nEnemyShotSfxID	= -1;
	m_fMovementTimer	= 0;
	m_fWaypoint01		= CGame::GetInstance()->GetWidth() * 0.9f;
	m_fWaypoint02		= CGame::GetInstance()->GetWidth() * 0.5f;
	m_fWaypoint03		= CGame::GetInstance()->GetWidth() * 0.1f;
	WP1					= true;
	WP2					= false;
	WP3					= false;

	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();

	//m_nEnemyShotSfxID = m_pXA->SFXLoadSound(_T("resource/sounds/...load sound here...") );
}

CFlyingEnemy::~CFlyingEnemy(void)
{
	//if( m_nEnemyShotSfxID != -1 )
	//{
	//m_pXA->SFXStopSound( m_nEnemyShotSfxID );
	//m_pXA->SFXUnloadSound( m_nEnemyShotSfxID );
	//m_nEnemyShotSfxID = -1;
	//}
}

//Inherited from CEntity///////////////////////////////
void CFlyingEnemy::Update	(float fElapsedTime)
{
	m_fShootTimer += fElapsedTime * 0.5f;

//CREATE FIRING BULLETS && THE SHOT SOUND===============
	if (this->m_fShootTimer > SHOT_DELAY)
	{
		//this->m_fShootTimer = 0.0f;
		//CCreateBulletMessage* pMsg = new CCreateBulletMessage(this);
		//m_pMS->SendMsg( pMsg );
		//pMsg = nullptr;

		//if(this->m_pXA->SFXIsSoundPlaying( m_nEnemyShotSfxID ) == false )
		//	this->m_pXA->SFXPlaySound( m_nEnemyShotSfxID );
	}
//CREATE WAYPOINTS AND MOVEMENT==========================
	if (this->m_fMovementTimer > MOVEMENT_DELAY)
	{
		if (FloatEquals(GetPosX(),m_fWaypoint01, 32) && WP1)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * 0.1f);
			WP2 = true;
			WP1 = false;
		}
		if (FloatEquals(GetPosX(), m_fWaypoint02, 32) && WP2)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * -0.1f);
			WP2 = false;
			WP3 = true;
		}
		if (FloatEquals(GetPosX(),m_fWaypoint03, 32) && WP3)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * 0.1f);
			WP2 = false;
			WP1 = true;
		}
	}

	CEntity::Update(fElapsedTime);

		if (FloatEquals(GetPosX(), m_fWaypoint02, 32) && WP2 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelX(0.0f);
		}
		if (FloatEquals(GetPosX(), m_fWaypoint03, 32) && WP3 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelY(0.0f);
		}
		if (FloatEquals(GetPosX(), m_fWaypoint01, 32) && WP1 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelX(0.0f);
		}
}

void CFlyingEnemy::Render	(void)
{
	m_pD3D->DrawRect(this->GetRect(), 0,0,255);
}

RECT CFlyingEnemy::GetRect(void) const
{
	RECT collision = {(int)this->GetPosX()- int(GetWidth()*0.5f), (int)this->GetPosY()-int(GetHeight()*0.5), (int)this->GetPosX()+ int(GetWidth()*0.5f), (int)this->GetPosY()+int(GetHeight()*0.5f)  };
	return collision;
}

bool CFlyingEnemy::CheckCollision( IEntity* pOther )
{
	CBullet* pBullet = dynamic_cast< CBullet* > (pOther);
	if (CEntity::CheckCollision(pOther))
	{
		if (pOther->GetType() == OBJ_BULLET)
		{
				//CDecreaseHealth* pMsg = new CDecreaseHealth(this, -10);
				//CSGD_MessageSystem::GetInstance()->SendMsg( pMsg );
				//pMsg = nullptr;
		}
		return true;
	}
	return false;
}
///////////////////////////////////////////////////////

//Inherited from CEnemy///////////////////////////////
void CFlyingEnemy::HandleEvent(CNEvent* pEvent)
{

}

*/